package com.roomio.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roomio.core.security.model.User;
import com.roomio.core.security.service.UserInfoService;
import com.roomio.dao.GroupInfoDAO;
import com.roomio.dao.UserInfoDAO;
import com.roomio.domain.Group;
import com.roomio.domain.UserGroup;
import com.roomio.mail.service.MailService;
import com.roomio.service.GroupInfoService;
import com.roomio.service.UserGroupService;

@Service
@Transactional
public class GroupInfoServiceImpl implements GroupInfoService {
	
	@Autowired
    private  MailService mailService;

	@Autowired
	private GroupInfoDAO groupInfoDAO  ;
	
	@Autowired
	private UserInfoService userInfoService  ;
	
	@Autowired
	private UserInfoDAO userInfoDAO;
	
	@Autowired
	private UserGroupService userGroupService;
	/**
	 * 
	 * @param grouName
	 * @param userName
	 * @return
	 */
	@Override
	public Group findGroupByNameAndUserName(String groupName, String userName){
		return groupInfoDAO.findGroupByNameAndUserName(groupName, userName);
	}
	
	/**
	 * 
	 * @param grouName
	 * @param userName
	 * @return
	 */
	@Override
	public Group findGroupByUser(String email){
		
		return null;//groupInfoDAO.findGroupByNameAndUserName(grouName, userName);
	}
	
	
	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#persist(com.roomio.domain.Group)
	 */
	@Override
	public Group persist(Group group) {
		List<String> listOfEmailTosend = new ArrayList<String>();
		for (Iterator iterator = group.getUsers().iterator(); iterator.hasNext();){
			UserGroup userGroup = (UserGroup) iterator.next();
			User u = userGroup.getUser();
			if(u.getId()==null){
				User tmpUser=userInfoDAO.findUserByEmail(u.getEmail());
				if(tmpUser==null){
					userInfoService.persist(u);					
				}
			}
		}
		groupInfoDAO.save(group);
		for (Iterator iterator = group.getUsers().iterator(); iterator.hasNext();) {
			UserGroup userGroup = (UserGroup) iterator.next();
			User user = userInfoService.findById(userGroup.getUser().getId());
			Map<Object , Object> mergeProperties = new HashMap<Object, Object>();
			mergeProperties.put("user", user);
	//		if(user.getIdentityUrl()== null){
				mailService.sendTemplateMail("RoomIO Invitation", "shailysinha2gmail.com",user.getEmail(), null, null, "invitaion.vm", mergeProperties);
		//	}
			
		}
		return group;
		
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#findGroupInfo(java.lang.Long)
	 */
	@Override
	public Group findGroupInfo(Long id_) {
		// TODO Auto-generated method stub
		return groupInfoDAO.get(id_);
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#findGroupInfoEntries(int, int)
	 */
	@Override
	public List<Group> findGroupInfoEntries(int firstResult, int sizeNo) {
		return groupInfoDAO.findAll(firstResult, sizeNo);
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#countGroupInfoes()
	 */
	@Override
	public int countGroupInfoes() {
		// TODO Auto-generated method stub
		return groupInfoDAO.findAll().size();
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#findAllGroupInfoes()
	 */
	@Override
	public List<Group> findAllGroupInfoes() {
		// TODO Auto-generated method stub
		return groupInfoDAO.findAll();
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#update(com.roomio.domain.Group)
	 */
	@Override
	public void update(Group group) {
		groupInfoDAO.update(group);
		
	}

	/* (non-Javadoc)
	 * @see com.roomio.service.impl.GroupInfoService#remove(java.lang.Long)
	 */
	@Override
	public void remove(Long id_) {
		groupInfoDAO.deleteById(id_);		
	}
	
	
	public List<Group> findGroupsByOwnerName(String userName){
		return groupInfoDAO.findGroupsByOwner(userName);
	}
	
	@Override
	public void addUserToGroup(Group group, String emailIdString) {
		/*
		 *1. Input of user list to add comes in as a comma separated string of email IDs.
		 *2. Find if all users admin is trying to add are present in the system.
		 *3. If the user is present in the system but not part of that group, add him/her.
		 *4. If user is a part of the system and also a part of the group, notify on screen.
		 *5. Users that are not the part of the system, notify on screen and send them emails to join the sytem.
		 **/
		List<String> emailIdList = new ArrayList<String>();
		String[] emailIdArray;
		User user = null;
		UserGroup userGroup = new UserGroup();
		emailIdArray = StringUtils.stripAll(StringUtils.split(emailIdString, ','));
		emailIdList = Arrays.asList(emailIdArray);
		
		for(String emailId : emailIdList) {
			user = userInfoDAO.findUserByEmail(emailId);
			if(null != user) {
				System.out.println("User with email " + emailId + " exists and will be added to group " + group.getName());
				userGroup.setUser(user);
				userGroup.setGroup(group);
				userGroupService.persist(userGroup);
			}
			else {
				/*
				 * Send email to user notifying group invite.
				 */
			}
		}
		
	}
}
